package com.zjw.zy.viewmodel

import android.graphics.BitmapFactory
import android.graphics.BitmapRegionDecoder
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.zjw.zy.base.BaseViewModel
import com.zjw.zy.bean.WsMessage
import com.zjw.zy.constant.C2CMessageInfo
import com.zjw.zy.ext.launchInIo
import com.zjw.zy.ext.requireService
import com.zjw.zy.setting.*
import com.zjw.zy.utils.GsonUtils
import com.zjw.zy.utils.PathUtils

/**
 * @author ：zhong.jw
 * @date ：Created in 2022/12/14 17:25
 */
class SettingViewModel : BaseViewModel() {

    init {
        SettingStorage.addOptionObserver(StartUpImagePathOption::class.java) {
            mStartUpImage.postValue(it as StartUpImagePathOption)
        }

        SettingStorage.addOptionObserver(LocationShareOption::class.java) {
            mLocationShare.postValue(it as LocationShareOption)
        }

        SettingStorage.addOptionObserver(WechatBackgroundPathOption::class.java) {
            mWechatBackgroundPathOption.postValue(it as WechatBackgroundPathOption)
        }

        SettingStorage.addOptionObserver(LoverNameOption::class.java) {
            mLoverName.postValue(it as LoverNameOption)
        }

        SettingStorage.addOptionObserver(SelfNameOption::class.java) {
            mSelfName.postValue(it as SelfNameOption)
        }
    }

    private val mSelfName =
        MutableLiveData<SelfNameOption>(
            SettingStorage.getOption(
                SelfNameOption::class.java
            )
        )

    fun getSelfName(): LiveData<SelfNameOption> = mSelfName

    private val mLoverName =
        MutableLiveData<LoverNameOption>(
            SettingStorage.getOption(
                LoverNameOption::class.java
            )
        )

    fun getLoverName(): LiveData<LoverNameOption> = mLoverName

    private val mStartUpImage =
        MutableLiveData<StartUpImagePathOption>(
            SettingStorage.getOption(
                StartUpImagePathOption::class.java
            )
        )

    fun getStartUpImage(): LiveData<StartUpImagePathOption> = mStartUpImage


    private val mLocationShare =
        MutableLiveData<LocationShareOption>(
            SettingStorage.getOption(
                LocationShareOption::class.java
            )
        )

    fun getLocationShare(): LiveData<LocationShareOption> {
        return mLocationShare
    }


    private val mWechatBackgroundPathOption =
        MutableLiveData<WechatBackgroundPathOption>(
            SettingStorage.getOption(
                WechatBackgroundPathOption::class.java
            )
        )

    fun getWechatBackgroundPathOption(): LiveData<WechatBackgroundPathOption> {
        return mWechatBackgroundPathOption
    }

    fun setStartUpImage(pathOption: StartUpImagePathOption) {
        launchInIo {
            SettingStorage.setOption(pathOption)

            val msg = WsMessage.obtainStrongC2C(C2CMessageInfo.START_UP_CHANGE_BACKGROUND)
            PathUtils.getBitmapBytesFromPath(pathOption.value)?.let {
                msg.text = GsonUtils.toJson(it)
                requireService()?.sendWsMessage(msg)
            }
        }
    }


    override fun onCleared() {
        super.onCleared()
    }

    fun setLocationShare(enable: Boolean) {
        launchInIo {
            SettingStorage.setOption(LocationShareOption().apply { value = enable })
        }
    }

    fun setWechatBackgroundImagePath(path: String) {
        launchInIo {
            SettingStorage.setOption(WechatBackgroundPathOption().apply { value = path })

            val msg = WsMessage.obtainStrongC2C(C2CMessageInfo.WECHAT_CHANGE_BACKGROUND)
            PathUtils.getBitmapBytesFromPath(path)?.let {
                msg.text = GsonUtils.toJson(it)
                requireService()?.sendWsMessage(msg)
            }
        }
    }

    fun setLoveName(name: String) {
        launchInIo {
            SettingStorage.setOption(LoverNameOption().apply { value = name })

            val msg = WsMessage.obtainStrongC2C(C2CMessageInfo.LOVER_NAME_SETTING)
            msg.text = name
            requireService()?.sendWsMessage(msg)
        }
    }
}